<?php
/**
 * Created by PhpStorm.
 * User: oven
 * Date: 2019/11/9
 * Time: 14:09
 */
class Dhsms {
    public static function create(){
        return new self;
    }

    //短信发送入口
    public function send($mobile,$content,$msgid){
        $ret = $this->sendSms( "{$mobile}",$content ,$msgid, date("Ymdhi"));
        return $ret;
    }

    private function sendSms($phones, $content, $msgid, $sendtime) {
        $data = array ('account' => XCYD_SMS_DH_ACCOUNT, 'password' => XCYD_SMS_DH_PWD, 'msgid' => $msgid, 'phones' => $phones, 'content' => $content, 'sign' => XCYD_SMS_DH_SIGN, 'subcode' => XCYD_SMS_DH_SUBCODE, 'sendtime' => $sendtime );
        return $this->post(XCYD_SMS_DH_URL . "/Submit", json_encode ( $data ) );
    }

    public function getSmsReport() {
        $data = array ('account' => XCYD_SMS_DH_ACCOUNT, 'password' => XCYD_SMS_DH_PWD );
        return $this->post(XCYD_SMS_DH_URL . "/Report", json_encode ( $data ) );
    }
    public function getSms() {
        $data = array ('account' => XCYD_SMS_DH_ACCOUNT, 'password' => XCYD_SMS_DH_PWD );
        return $this->post(XCYD_SMS_DH_URL . "/Deliver", json_encode ( $data ) );
    }
    private function post($url, $data){
            $ch = curl_init ( $url );
            curl_setopt ( $ch, CURLOPT_POST, 1 );
            curl_setopt ( $ch, CURLOPT_HEADER, 0 );
            curl_setopt ( $ch, CURLOPT_FRESH_CONNECT, 1 );
            curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt ( $ch, CURLOPT_FORBID_REUSE, 1 );
            curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
            curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data ) ) );
            curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
            $ret = curl_exec ( $ch );
            curl_close ( $ch );
            return $ret;
    }
}